package dr.joketalker.controller;

import java.util.LinkedList;
import java.util.List;


import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

import dr.joketalker.R;
import dr.joketalker.Debug.DebugManager;
import dr.joketalker.controller.view.GridItem;
import dr.joketalker.controller.view.GridViewAdapter;
import dr.joketalker.dao.FactoryDAO;
import dr.joketalker.dao.JokeDAO;
import dr.joketalker.entity.Joke;

public class MainController extends MyController{


	protected GridItem giSearch;
	protected GridItem giJoke;
	protected GridItem giProfil;
	protected GridItem giCategory;
	protected GridItem giRecord;

	protected GridView gdvMain;
	
	
	
	
	
	protected JokeDAO jokeDao = FactoryDAO.getJokeDao();
	
	@Override
	 public void onCreate(Bundle savedInstanceState) {
	 
	        super.onCreate(savedInstanceState);
	        

            setContentView(R.layout.main);

	        /*
	        LinearLayout layout = (LinearLayout)findViewById(R.id.llMain);
	        // Create the adView
	        // Please replace MY_BANNER_UNIT_ID with your AdMob Publisher ID
	        AdView adView = new AdView(this, AdSize.BANNER, MY_BANNER_UNIT_ID);
	        // Add the adView to it
	        layout.addView(adView);
	        // Initiate a generic request to load it with an ad
	        AdRequest request = new AdRequest();
	        request.setTesting(true);
	        adView.loadAd(request);  
	        */
	        
	        gdvMain = (GridView)findViewById(R.id.gdvMain);
	        
	        
	     // gdvMain.setExpanded(true);
	        GridViewAdapter gdvAdapter = new GridViewAdapter(this);
	        

	        List<GridItem> listGridItem = new LinkedList<GridItem>();
	        giRecord = new GridItem(this,R.string.record,R.drawable.record);
	        listGridItem.add(giRecord);
	        giSearch = new GridItem(this,R.string.search,R.drawable.search);
	        listGridItem.add(giSearch);
	        
	        giProfil = new GridItem(this,R.string.profil,R.drawable.profil);
	        listGridItem.add(giProfil);
	        
	        giJoke = new GridItem(this,R.string.dayJoke,R.drawable.star);
	        listGridItem.add(giJoke);
	        
	        giCategory = new GridItem(this,"category",R.drawable.ic_launcher);
	        listGridItem.add(giCategory);
	        
	        for(GridItem gi : listGridItem){
	        	gdvAdapter.addItem(gi);
	        }


	        gdvMain.setOnItemClickListener(this);

	        gdvMain.setAdapter(gdvAdapter);
	           
	        DebugManager.getInstance();
	             
	        
	        StrictMode.ThreadPolicy policy = new StrictMode.
	        		ThreadPolicy.Builder().permitAll().build();
	        		StrictMode.setThreadPolicy(policy);  
	        		
	        		init();
	 }


	
	@Override
	protected void init() {

		super.init();
	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {

		
		if(giRecord.equals(v)){
			

			
			
		Intent intent = new Intent(this, VoiceRecorderController.class);
			startActivity(intent);		
		}else if(giSearch.equals(v)){
			Toast.makeText(this,"ne ne ne", Toast.LENGTH_SHORT).show();
//			Intent intent = new Intent(this, SearchController.class);
//			startActivity(intent);
		}else if(giJoke.equals(v)){
			Toast.makeText(this,"ne ne ne", Toast.LENGTH_SHORT).show();
//			Joke joke = jokeDao.read(60);
//			
//			Intent intent = new Intent(this, JokeController.class);
//			intent.putExtra("JOKE", joke);
//		
//			startActivity(intent);
			
		}else if(giProfil.equals(v)){
			Toast.makeText(this,"ne ne ne", Toast.LENGTH_SHORT).show();
//			Intent intent = new Intent(this, ProfilController.class);
//			startActivity(intent);			
		}else if(giCategory.equals(v)){
			Intent intent = new Intent(this, CategoryController.class);
			startActivity(intent);			
		}
		
	}
	 
}
